package com.gdpu.liou.symphonyserver.controller;

import com.gdpu.liou.symphonyserver.entity.respose.Response;
import com.gdpu.liou.symphonyserver.entity.respose.ResponseCode;
import com.gdpu.liou.symphonyserver.service.RankService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/grade")
@Slf4j
public class RankController {
    private final RankService rankService;

    public RankController(RankService rankService) {
        this.rankService = rankService;
    }


    /**
     * 用户给歌单打分
     */

    @RequestMapping(value = "/gradePlaylist")
    public Response gradePlaylist(@RequestParam Long playlistId,
                                  @RequestParam Long consumerId,
                                  @RequestParam Integer score) {
        boolean b = rankService.gradePlaylist(playlistId, consumerId, score);
        return new Response<>(b);
    }

    /**
     * 获取歌单得分排行榜
     */

    @RequestMapping(value = "/selectScoreOfRankingOfPlaylist", method = RequestMethod.GET)
    public Response rankOfSongListId() {

            List<Map<String, String>> maps = rankService.selectAvgScoreOfPlaylist();
            if (CollectionUtils.isEmpty(maps)) {
                return new Response<>();
            }
            return new Response<>(maps, maps.size());
    }

    /**
     * 查询用户对此歌单的评分
     */

    @RequestMapping(value = "/selectGradeOfPlaylistOfConsumer", method = RequestMethod.GET)
    public Response selectGradeOfPlaylistOfConsumer(@RequestParam Long consumerId, @RequestParam Long playlistId) {
            Double aDouble = rankService.selectGradeOfPlaylistOfConsumer(playlistId, consumerId);
            if (aDouble == null) {
                return new Response<>(0);
            }
            return new Response<>(aDouble);
    }
}
